<div class="row animate fadeIn">
    <form id="edit_users_form" class="form-horizontal"  method="post">
      <?php foreach($users_data as $user) { ?>
      <input type="hidden" name="hidden_user" id="hidden_user_id" value="<?php echo $user->user_id; ?>" >
        <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Edit Users</i></h5>
            <div class="ibox-tools">    
                <button  id="sads" class="btn btn-sm btn-primary save-btn-tab">Save</button>
                <a id = "cancel_edit" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a>
            </div>
        </div>
        <div class="ibox-content">
          <div class="form-group">
              <label class="col-lg-4 control-label">User Name</label>
            <div class="col-lg-5">
              <input type="text" id="e_username" name="e_usr_name" class="form-control"  value="<?php echo $user->user_name; ?>">
            </div>
          </div>
          <div class="form-group" id="e_fname">
              <label class="col-lg-4 control-label">First Name</label>
            <div class="col-lg-5">
              <input type="text" name="e_f_name" class="form-control" value="<?php echo $user->first_name; ?>">
            </div>
          </div>
          <div class="form-group" id="e_lname">
              <label class="col-lg-4 control-label">Last Name</label>
            <div class="col-lg-5">
              <input type="text" name="e_l_name" class="form-control" value="<?php echo $user->last_name; ?>">
            </div>
          </div>
          <div class="form-group password">
              <label class="col-lg-4 control-label" >Password</label>
            <div class="col-lg-5">
              <input type="password" name="e_pwd" class="form-control" value="<?php echo $user->pwd; ?>" id="e_passwd" readonly>
            </div>
          </div>
          <div class="form-group password">
              <label class="col-lg-4 control-label">New Password</label>
            <div class="col-lg-5">
              <input type="password" name="n_e_pwd" class="form-control" value="" id="n_e_passwd">
            </div>
          </div>
          <div class="form-group password" >
              <label class="col-lg-4 control-label">Confirm Password</label>
            <div class="col-lg-5">
              <input type="password" name="e_cfmpasswd" class="form-control">
            </div>
          </div>
          <div class="form-group password">
              <label class="col-lg-4 control-label">Email</label>
            <div class="col-lg-5">
              <input type="text" name="e_email" class="form-control" value="<?php echo $user->email; ?>">
            </div>
          </div>
          <div class="form-group">
              <label class="col-lg-4 control-label">Company</label>
            <div class="col-lg-5">
              <select  id="e_comp_id" class="form-control chosen-select" name="e_company_id">
                <?php foreach ($company as $comp) { ?>
                  <option value="<?php echo $comp->company_id; ?>" <?php echo($comp->company_id == $user->company_id ? 'selected' : ''); ?> ><?php echo $comp->company_name; ?></option>
                <?php } ?>
              </select>
            </div>
          </div>
          <div class="form-group">
            <label class="col-lg-4 control-label">Department</label>
            <div class="col-lg-5">
              <select  id="" class="form-control chosen-select" name="department">
                <option value="">--</option>
                <?php foreach ($department as $dep) { ?>
                  <option value="<?php echo $dep->lookup_code; ?>" <?php echo ($dep->lookup_code == $user->department ? 'selected' : ''); ?>><?php echo $dep->meaning; ?></option>
                <?php } ?>
              </select>
            </div>
          </div>
          <div class="form-group">
              <label class="col-lg-4 control-label">Enable</label>
            <div class="col-lg-5">
              <input type="checkbox" value="ok" name="e_usr_ena" id="" class="i-checks" checked>
            </div>
          </div>
        </div>
       <?php } ?> 
    </form>
<!--  -->
        <div id="" class="ibox-content">
            <h3 style="color: #00afef">Assign Role Access</h3>
              <form  id="edit_role_access" class="">
                <input type="hidden" name="role_hidden_user_id" id="e_h_usr_id">
            </br>
              <table id="pgTable"class="table table-bordered" style="width:80%;">
                
                <thead>
                    <tr>
                      <th style="">Role</th>
                      <th style="width:130px;text-align:right;">Start Date</th>
                      <th style="width:130px;text-align:right;">End Date</th>
                      <th style="width:50px;">Enable</th>
                      <th style="width:80px;text-align:center;">Action</th>
                    </tr>
                    <tr class="input-daterange" id="datepicker">
                      <td style="margin:0px;padding:0px;">
                          <select id="" class="chosen-select" name="role_usr_id">
                                <?php foreach($roles as $role) { ?>
                                    <option value="<?php echo $role->role_id; ?>"> <?php echo $role->role_name; ?> </option>
                                <?php } ?>
                            </select>
                      </td>
                      <td style="margin:0px;padding:0px;">
                                
                          <input style="width:100%;" type="search" id="rustart" name="role_usr_start" class="form-control-custom">
                      </td>
                      <td style="margin:0px;padding:0px;">
                          <input style="width:100%;" type="search" id="" name="role_usr_end" class="form-control-custom">
                      </td>
                      <td style="padding:5px;text-align:center;">
                          <input type="checkbox" value="ok" name="role_usr_ena" id="" class="i-checks" checked>
                      </td>
                      <td style="margin:0px;padding:0px;">
                          <button style="width:100%;" class="btn btn-white" id=""><i class="fa fa-plus success"></i></button>
                      </td>
                    </tr>    
                </thead>
              </form>
                <tbody id="tbl_roleEdit" class="animate fadeIn">                               
                </tbody>
              </table>
               
        </div>
</div>
<script>
$(document).ready(function () {$('#s-c').remove();
    $('select').trigger('chosen:updated');
    $('#edit_users_form').data('serialize',$('#edit_users_form').serialize());
    $(".chosen-select").chosen({width:"100%;"});
    setDates();
    var setter = 0;
    var aw = 0;
    var current_start_date;
    var current_end_date;
    var u_id = $('#hidden_user_id').val();
    $('#e_h_usr_id').val(u_id);
      $('.i-checks').iCheck({
          checkboxClass: 'icheckbox_square-green',
          radioClass: 'iradio_square-green',
      });
    
      $('.input-daterange').datepicker({
                keyboardNavigation: false,
                forceParse: false,
                autoclose: true,
                format: "yyyy-mm-dd"
      });
     var pickers = {autoclose: true, format: "yyyy-mm-dd"};
      function setDates(){
        var today = new Date();
        $("#rustart").val(pad(today.getFullYear())+'-'+pad(today.getMonth() + 1)+'-'+pad(today.getDate()));
      }

     function pad(numb) {
          return (numb < 10 ? '0' : '') + numb;
      }



     $.validator.setDefaults({ ignore: ":hidden:not(select)" })
     $.ajax({
            type: "POST",
            url: "<?php echo base_url($this->session->userdata('forajax').'/view_roles_access'); ?>"+"/"+u_id,
            dataType: 'json',
        success: function(data){
             $('#tbl_roleEdit').empty();
                var trHTML = ''; var cb_ena ="hover";
                $.each(data, function(i, item) {
                  if(data[i].enable_flag == "Y"){
                            cb_ena = "checked";
                    }
                    trHTML += '<tr class="input2-daterange" id="datepicker"><input type="hidden" id="i_role_id" value="'+data[i].role_id+'"><td>' + data[i].role_name + '</td><td id="s_d" text-align:right;><span>' + data[i].start_date + '</span><input style="width:100%;" class="form-control-custom" type="search"  id="isd" name="inline_sd" value="'+data[i].start_date+'"></td><td id="e_d" text-align:right;><span>' + data[i].end_date + '</span><input style="width:100%;" class="form-control-custom" type="search" id="ied" name="inline_ed" value="'+data[i].end_date+'"></td><td style="padding:5px;text-align:center;"><div style="cursor:default;" class="state icheckbox_square-green '+cb_ena+'"></div></td><td id="usr_btn" style="margin:0px;padding:0px;"><a style="width:100%;" id="to_edit" class="btn btn-outline btn-info edit_u" id="gana"><i id="e_usr_icon" class="fa fa-paste success"></i></a></td></tr>';                 
                  $('.input-daterange').datepicker(pickers); 
                });
                $('#tbl_roleEdit').append(trHTML);
                $('#tbl_roleEdit input').hide();
                }      
            });
    


      $(document).on('click','.btn.btn-outline.btn-info.edit_u',function(){
              if(setter == 0){
                current_start_date = $(this).closest('tr').children('#s_d').find('span').html();
                current_end_date = $(this).closest('tr').children('#e_d').find('span').html();
                $(this).closest('tr').children('#s_d').find('span').hide();
                $(this).closest('tr').children('#e_d').find('span').hide();

                $(this).closest('tr').find('#isd').val(current_start_date);
                console.log($(this).closest('tr').find('#isd').val());
                $(this).closest('tr').find('#ied').val(current_end_date);
                $(this).closest('tr').find('#isd , #ied').show();
                $(this).closest('.input2-daterange input:nth(0)').datepicker('update', new Date(current_start_date));
                $(this).closest('.input2-daterange').datepicker(pickers);

                
                $(this).closest('tr').children('#usr_btn').append('<a id="cancel_edit" class = "btn btn-outline btn-warning cancel"><i class="fa fa-undo success"></i></a><a id="save_edit" class = "btn btn-outline btn-info save"><i class="fa fa-check success"></i></a>')
                $(this).closest('tr').children('#s_d , #e_d').css({"margin":0 , "padding":0});
                $(this).remove();
                
                
                setter = 1;
                }else{
                  // alert_cannot_edit();
                } 
                  
          });
      $(document).on('click','.btn.btn-outline.btn-warning.cancel',function(){
                  console.log(current_start_date);
                $(this).closest('tr').find('#isd , #ied').hide();
                $(this).closest('tr').children('#usr_btn').append('<a style="width:100%;" id="to_edit" class="btn btn-outline btn-info edit_u" id="gana"><i id="e_usr_icon" class="fa fa-paste success"></i></a>');
                //$('.input2-daterange').datepicker(pickers).remove();
                $(this).closest('tr').children('#s_d').find('span').text(current_start_date).show();
                $(this).closest('tr').children('#e_d').find('span').text(current_end_date).show();
                $(this).closest('tr').children('#s_d , #e_d').css({"padding":8});
                $(this).closest('tr').find('#cancel_edit , #save_edit').remove();
                setter = 0;
                
      });

      $(document).on('click','.btn.btn-outline.btn-info.save',function(){

        var same = $(this);
        var start_new = $(this).closest('tr').find('#isd').val();
        var end_new  = $(this).closest('tr').find('#ied').val();
        var role_id   = $(this).closest('tr').children('#i_role_id').val();
        $.ajax({
                  type: "POST",
                  url: "<?php echo base_url($this->session->userdata('forajax').'/update_inline_role_access'); ?>",
                  data: {"start": start_new ,"end": end_new , "role_id": role_id , "user_id": u_id },
                  dataType: 'json',
              success: function(data){ 
                      if(data[0].error == 1){
                          notifymsg("danger",data[0].msg); 
                        }else{
                          notifymsg("success",". . .Data Updated");
                          $(same).closest('tr').find('#isd , #ied').hide();
                          $(same).closest('tr').children('#usr_btn').append('<a style="width:100%;" id="to_edit" class="btn btn-outline btn-info edit_u" id="gana"><i id="e_usr_icon" class="fa fa-paste success"></i></a>');
                          //$('.input2-daterange').datepicker(pickers).remove();
                          $(same).closest('tr').children('#s_d').find('span').text(start_new).show();
                          $(same).closest('tr').children('#e_d').find('span').text(end_new).show();
                          $(same).closest('tr').children('#s_d , #e_d').css({"padding":8});
                          $(same).closest('tr').find('#cancel_edit , #save_edit').remove();
                          setter = 0;
                        }
                      }      
                  });
       
      });

      $('#edit_role_access').validate({
        rules : {
          role_usr_id  : {
            required: true
          }
        },submitHandler: function(form){
          dataString = $("#edit_role_access").serialize();
          $.ajax({
            type: "POST",
            url: "<?php echo base_url($this->session->userdata('forajax').'/update_role_access'); ?>",
            data: dataString,
            dataType: 'json',
          success: function(data){
            if(data[0].error == 1){
                notifymsg("danger",data[0].msg);
            }else{
                notifymsg("success",". . .User Role Added Successfully");
                $('#tbl_roleEdit').empty();
                var trHTML = ''; var cb_ena ="hover";
                $.each(data, function(i, item) {
                  if(data[i].enable_flag == "Y"){
                            cb_ena = "checked";
                    }
                    trHTML += '<tr class="input2-daterange" id="datepicker"><input type="hidden" id="i_role_id" value="'+data[i].role_id+'"><td>' + data[i].role_name + '</td><td id="s_d" text-align:right;><span>' + data[i].start_date + '</span><input style="width:100%;" class="form-control-custom" type="search"  id="isd" name="inline_sd" value="'+data[i].start_date+'"></td><td id="e_d" text-align:right;><span>' + data[i].end_date + '</span><input style="width:100%;" class="form-control-custom" type="search" id="ied" name="inline_ed" value="'+data[i].end_date+'"></td><td style="padding:5px;text-align:center;"><div style="cursor:default;" class="state icheckbox_square-green '+cb_ena+'"></div></td><td id="usr_btn" style="margin:0px;padding:0px;"><a style="width:100%;" id="to_edit" class="btn btn-outline btn-info edit_u" id="gana"><i id="e_usr_icon" class="fa fa-paste success"></i></a></td></tr>';
                });
                setter = 0;
                $('#tbl_roleEdit').append(trHTML);
                $('input[name="role_usr_id"] option:first').attr('selected','selected').trigger('chosen:updated');
                $('#tbl_roleEdit input').hide();
                $('#edit_role_access')[0].reset();
                setDates();
            }
          }      
         });   
        }
      });

     $.validator.setDefaults({ ignore: ":hidden:not(select)" })
     $('#edit_users_form').validate({
      rules :{
        e_usr_name: {
          required : true
        },
        e_f_name:    {
          required : true
        },
        e_l_name:    {
          required : true
        },
        e_pwd:    {
          required : true
        },
        e_cfmpasswd:    {
          equalTo: "#n_e_passwd"
        },
        e_email: {
          required : true,
          email: true
        },
        e_comp_id:   {
          required : true
        }
      },submitHandler: function(form){
          dataString = $("#edit_users_form").serialize();
          $.ajax({
            type: "POST",
            url: "<?php echo base_url($this->session->userdata('forajax').'/update_users'); ?>",
            data: dataString,
            dataType: 'json',
          success: function(data){
            if(data[0].error == 1){
                notifymsg("danger",data[0].msg);
            }else{
                notifymsg("success",". . .User Updated Successfully");
                close_edit(); active_search();
            }
          }      
         });   
        }
      }); 




        function alert_cancel(){
            if($('#edit_users_form').serialize()!=$('#edit_users_form').data('serialize')){
                swal({
                    title: "Are you sure?",
                    text: "Your work will not be save",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "Leave",
                    cancelButtonText: "Stay",
                    closeOnConfirm: true,
                    closeOnCancel: true },
                function (isConfirm) {
                    if (isConfirm) {
                        close_edit(); active_search();
                        notifymsg("hide","hide");
                    } else {
                        swal("Cancelled");
                    }
                });
                }else{
                close_edit(); active_search();
                notifymsg("hide","hide");
            }
        } 

       

        $('#cancel_edit').click(function () {          
            alert_cancel();
         });
        $('#editX').click(function(){
            alert_cancel();        
       });

});
</script>